<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
error_reporting(ALL);
$res_url="/phoenixResource/resource/res-type/4/id/";
//资源子系统
//$res_s_url="http://127.0.0.1:8081";
//$url = "http://192.168.2.119:8080";
$res_s_url="http://protected-api.nj.res.tuniu.org";


//搜索
$url = "http://127.0.0.1:8084";
$url = "http://10.10.30.44:8080";

//$url="http://public-api.pla.tuniu.org";
if (! $_GET ['op']) {
	$path = "/ror/phoenix/product/update";
	$method = 'POST';
	//
	$conn = mysql_connect ( "10.10.10.82:3306", "getmeixuesong", "tuniu520" );
	// mysql_select_db ( "prd_nm" );
	//$conn = mysql_connect ( "192.168.1.218:3310", "prd_nmtest", "tuniu520" );
	mysql_select_db ( "prd_nm" );
	//$conn = mysql_connect ( "192.168.1.123:3306", "prddev", "tuniu520" );
	mysql_query ( "set names utf8" );
	
	//mysql_select_db ( "phoenix_product" );
	$sql = "SELECT p.name productName,p.base_id,p.id productId,p.owner_id ownerId,p.owner_name ownerName,p.manager_id managerId,
				   p.manager_name managerName,p.op_time opTime,p.op_uid opUid,p.op_name opName,p.status,p.departure_city_code departCityCode,
				   p.lowest_price tuniuPrice,p.cash_backed cashBacked,b.grade,b.pro_type productType,b.deadline_day deadlineDay,
				   b.deadline_time deadlineTime,p.op_memo remark 
			FROM product p LEFT JOIN product_base b ON p.base_id=b.id order by p.id";
	$query = mysql_query ( $sql );
	$product = $prodcuts = array ();
	while ( $product = mysql_fetch_assoc ( $query ) ) {
		$product['isShow']=0;
		$product['remark']=str_replace(array("\n","\t","\r"),array("<br />","","<br />"),addslashes($product['remark']));
		$product_resouces=array();
		//取行程等信息
		$sql = "SELECT j.id, jd.destination_code,r.res_id 
				FROM product_journey j 
				JOIN product_res r ON j.id=r.journey_id 
				JOIN product_journey_destination jd ON j.id=jd.journey_id
				WHERE j.del_flag=0 AND r.del_flag=0 AND jd.del_flag=0 AND j.base_id={$product['base_id']}  LIMIT 1";
		$res = GetRow ( $sql, &$conn );
		/*
		 * foreach ($res as &$v){ foreach ($v as $kk=>$vv)
		 * $v[$kk]=urlencode($vv); } $product ['resource'] =
		 * urldecode(json_encode ( $res));
		 */
		
		$journey[0]['journeyId']=$res['id'];
		//取资源详情
		if($res['res_id']){
			$resource=json_decode(base64_decode(file_get_contents ( $res_s_url . $res_url.$res['res_id']."?".base64_encode ( ( json_encode ( array('resId'=>$res['res_id']) ) ) ), 0 )),true);
			$resource=$resource['data'];
			$product_resouces[]=urlencode(addslashes($resource['name']));
			$product_resouces[]=urlencode(addslashes($resource['spotName']));
			$resource['tuniuPrice']=$product['tuniuPrice'];
			foreach ($resource as $k=>$v){
				$resource[$k]=urlencode(str_replace(array("\n","\t","\r"),array("<br />","","<br />"),addslashes($v)));
//				$resource[$k]=urlencode(addslashes($v));
				//$resource[$k]=urlencode(str_replace("\t", "", str_replace("\r","",str_replace("\n"," ",str_replace("\"","\\\"",addslashes($v))))));
				//$resource[$k]=urlencode(str_replace("\t","",str_replace("\n\r","",htmlspecialchars($v))));
			}
			//$resource_str=urldecode("[".str_replace("\"", "\\\"", json_encode($resource))."]");
			
		}
		
		//$product['journey']="[{\\\"journeyId\\\":".$res['id'].",\\\"resource\\\":".$resource_str."}]";//urldecode(json_encode($journey));
		$product['journey']=addslashes(urldecode(json_encode(array(array("journeyId"=>$res['id'],"resource"=>array($resource))))));
		/* $resources = array ();
		foreach ( $res as $k => $v ) {
			$sql = "SELECT * FROM phoenix_resource.menpiao WHERE id=" . $v ['resourceId'];
			// echo $sql;die();
			$reosuce = mysql_fetch_assoc ( mysql_query ( $sql ) );
			foreach ( $reosuce as $k => $v ) {
				$reosuce [$k] = urlencode ( $v );
			}
			$resources [] = $reosuce;
		}
		$product ['resourceDetail'] = urldecode ( json_encode ( $resources ) ); */
		
		$product['productName']=addslashes($product['productName']);
		foreach ( $product as $k => $v ) {
			$product [$k] = urlencode ( $v );
		}
		$product['resource']=$product_resouces;
		if($res['destination_code']){
			$product['destinationCode'][]=$res['destination_code'];
		} 
		$prodcuts [] = $product;
	}
} else {
	$path = "/ror/phoenix/product/delete";
	$method = 'POST';
	// 删除
	for($i = 1; $i < 10; $i ++) {
		$prodcuts [] = $i;
	}
}
// var_dump($prodcuts[0]);
// var_dump ( urldecode ( json_encode ( $prodcuts ) ) );
$prodcuts=array_chunk ($prodcuts, 50);
foreach ($prodcuts as $v){
	//var_dump(urldecode( json_encode ( $v ) ) );
	$context = stream_context_create ( array (
			'http' => array (
					'method' => $method,
					'content' => base64_encode ( urldecode ( json_encode ( $v ) ) )
			)
	) );
//sleep(1);
	 $end=json_decode(base64_decode(file_get_contents ( $url . $path, 0, $context ) ),true);
	 if(!$end['success']){
		 echo "<br>************************************************************<br>";
		var_dump( urldecode ( json_encode ( $v ) ) );
		 echo "<br>************************************************************<br>";
	 }else{
		echo "<br>{$end['success']}<br>";
	 }
 }

// var_dump ( $prodcuts );

function GetPlan($sql, &$conn) {
	
	$rs = mysql_query ( $sql, $conn );
	$data = array ();
	while ( ($row = mysql_fetch_assoc ( $rs )) != false ) {
		$data [] = $row;
	}
	mysql_free_Result ( $rs );
	return $data;
}
function GetRow($sql, &$conn) {
	$rs = mysql_query ( $sql, $conn );
	$row = mysql_fetch_assoc ( $rs );
	@mysql_free_Result ( $rs );
	return $row;
}
function Query($sql) {
	$res = mysql_query ( $sql );
	return $res;
}
?>